<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Poetry extends Model {

    //
    protected $table = 'poetry';


    public static $TAG_MAP = [
        1 => "先秦",
        2 => "秦代",
        3 => "汉代",
        4 => "魏晋",
        5 => "南北朝",
        6 => "隋代",
        7 => "唐诗",
        8 => "五代",
        9 => "宋词",
        10 => "辽代",
        11 => "金代",
        12 => "元曲",
        13 => "明代",
        14 => "清代",
        15 => "近代",
        16 => "现代",
        17 => "当代",
    ];


    /**
     * 根据标题查询诗词内容
     * @param $title
     * @param null $author
     * @param int $offset
     * @param int $size
     * @return
     */
    public static function queryByTitle($title, $author = null, $offset = 0, $size = 20) {

        $builder = Poetry::where('title', '=', $title);

        if (!is_null($author)) {
            $builder = $builder->where('author', '=', $author);
        }

        $builder = $builder->where('is_deleted', '=', 0);

        if ($offset > 0) {
            $builder = $builder->skip($offset);
        }

        return $builder->take($size)->get();
    }


    /**
     * 根据内容查询诗词
     *
     * @param $content 待匹配的内容
     * @param int $offset 偏移量
     * @param int $size 每次查询的数量
     * @return mixed
     */
    public static function queryByContent($content, $offset = 0, $size = 20) {
        return Poetry::where('content', "like", '%' . $content . '%')->skip($offset)->take($size)->get();
    }

}
